﻿using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;
using System.Text;

class SimpleTcpServer
{
    const int LISTENING_PORT = 2222;
    const string EXIT_COMMAND = "exit";

    static void Main()
    {
        StartServer();

        TrySendData();
    }
  
    private static void TrySendData()
    {
        TcpClient tcpClient = new TcpClient();
        try
        {
            tcpClient.Connect("localhost", LISTENING_PORT);
        }
        catch (SocketException)
        {
            Console.WriteLine("Error: Unable to connect to the server.");
            return;
        }

        using (tcpClient)
        {
            NetworkStream netStream = tcpClient.GetStream();
            using (netStream)
            {
                // Send HTTP GET request to the Web server
                try
                {
                    StreamWriter writer = new StreamWriter(netStream);
                    writer.WriteLine("Pesho");
                    writer.WriteLine();
                    writer.Flush();
                }
                catch (IOException)
                {
                    Console.WriteLine("Error: Can not send request.");
                    return;
                }

                // Receive the HTTP answer from the server
                try
                {
                    byte[] buffer = new byte[4096];
                    while (true)
                    {
                        int bytesRead = netStream.Read(buffer, 0, buffer.Length);
                        if (bytesRead == 0)
                        {
                            // Server closed the connection
                            break;
                        }
                        string data = Encoding.ASCII.GetString(buffer, 0, bytesRead);
                        Console.Write(data);
                    }
                }
                catch (IOException)
                {
                    Console.WriteLine("Error: Can not read the server response.");
                }
            }
        }
    }
  
    private static void StartServer()
    {
        IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Any, LISTENING_PORT);
        TcpListener server = new TcpListener(serverEndPoint);
        server.Start();
        Console.WriteLine("Simple TCP Server started listening on {0}...", server.LocalEndpoint);
          
        Task.Factory.StartNew(() =>
        {
            while (true)
            { 
                //This will block the execution of the program.
                TcpClient client = server.AcceptTcpClient();
                //
                Console.WriteLine("Accepted client connection.");
                NetworkStream netStream = client.GetStream();
                using (netStream)
                {
                    StreamWriter writer = new StreamWriter(netStream);
                    StreamReader reader = new StreamReader(netStream);
                    string name = reader.ReadLine();
                    if (name == EXIT_COMMAND)
                    {
                        break;
                    }
                    writer.WriteLine("Hello, " + name);
                    writer.Flush();
                }
                client.Close();
                Console.WriteLine("Client connection closed.");
            }
            server.Stop();
            Console.WriteLine("Simple TCP Server stopped.");
        });
    }
}